﻿namespace Okazuki.TepcoUsageApi
{
    using System.Collections.Generic;
    using System.IO;
    using System.Runtime.Serialization.Json;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using Okazuki.TepcoUsageApi.Client;

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            var client = new TepcoCapacityClient();
            client.GetLatest(result =>
                {
                    this.Dispatcher.BeginInvoke(() =>
                        {
                            var s = new DataContractJsonSerializer(typeof(TepcoStatus));
                            var ms = new MemoryStream();
                            s.WriteObject(ms, result);
                            var text = Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length);
                            MessageBox.Show(text);
                        });
                });
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            var client = new TepcoCapacityClient();
            client.GetStatusByDateTime(2011, 3, 23, 13, result =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    var s = new DataContractJsonSerializer(typeof(TepcoStatus));
                    var ms = new MemoryStream();
                    s.WriteObject(ms, result);
                    var text = Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length);
                    MessageBox.Show(text);
                });
            });
        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            var client = new TepcoCapacityClient();
            client.GetStatysByDate(2011, 3, 23, result =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    var s = new DataContractJsonSerializer(typeof(IList<TepcoStatus>));
                    var ms = new MemoryStream();
                    s.WriteObject(ms, result);
                    var text = Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length);
                    MessageBox.Show(text);
                });
            });
        }

        private void button4_Click(object sender, RoutedEventArgs e)
        {
            var client = new TepcoCapacityClient();
            client.GetStatusByMonth(2011, 3, result =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    var s = new DataContractJsonSerializer(typeof(IList<TepcoStatus>));
                    var ms = new MemoryStream();
                    s.WriteObject(ms, result);
                    var text = Encoding.UTF8.GetString(ms.ToArray(), 0, ms.ToArray().Length);
                    MessageBox.Show(text);
                });
            });
        }

        private void button5_Click(object sender, RoutedEventArgs e)
        {
            var client = new TepcoCapacityClient();
            client.GetStatusByDateTime(2000, 3, 23, 13, result =>
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    MessageBox.Show((result == null).ToString());
                });
            });
        }
    }
}
